package cn.sciento.farm.devicemanager.dao.config;

import com.google.common.collect.Range;
import io.shardingsphere.api.algorithm.sharding.RangeShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.RangeShardingAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.LinkedHashSet;

/**
 * @author wumu
 * @version V1.0
 * @date 7/5/18
 * @time 2:53 PM
 */
public class HistoryRangeShardingAlgorithm implements RangeShardingAlgorithm<Long> {

    Logger logger = LoggerFactory.getLogger(getClass());

    private long count = 1000*3600*24L;

    private String tableName = "dev_collection_history_";

    private long  EIGHT_HOURS = 8*3600*1000L;

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> shardingValue) {
        Collection<String> result = new LinkedHashSet<>();
        Range<Long> range = (Range<Long>) shardingValue.getValueRange();
        System.out.println("最小值："+range.lowerEndpoint());
        System.out.println("最大值："+range.upperEndpoint());

        Long lowerEndPoint = range.lowerEndpoint() +EIGHT_HOURS;
        Long upperEndPoint = range.upperEndpoint() +EIGHT_HOURS;
        Long lowerId = (lowerEndPoint)/count;
        Long betweenVal = upperEndPoint - lowerEndPoint-1;
        long idNum  = betweenVal/count;
        for(int i =0;i<=idNum;i++){
            long id = lowerId+i;
            logger.info("查询的历史表名："+tableName+id);

            result.add(tableName+id);
        }
        return result;
    }
}
